/*
 * Copyright 2008 Georgi Staykov
 * 
 * This file is part of pscoder.
 *
 * pscoder is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * pscoder is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with pscoder.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gstaykov.pscoder.editor.completion;

import java.util.TreeMap;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

import com.gstaykov.pscoder.Activator;
import com.gstaykov.pscoder.preferences.PreferenceConstants;
import com.gstaykov.pscoder.util.Util;

public class PowershellCompletionProcessor implements IContentAssistProcessor {
	
    private ICompletionProposal[] emptyProposal = new ICompletionProposal[0];
    
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
	    CompletionDictionary dict = null;
	    IResource activeFile = Util.getActiveFile();
	    if (activeFile == null) {
	        // Cannot locate this active file return no completion
	        return emptyProposal;
	    }
	    String activeFileName = activeFile.getLocation().toFile().getAbsolutePath();
	    
		try {
			dict = CompletionDictionary.getInstance(activeFile.getProject().getName());
		} catch (CoreException ce) {
			ce.printStackTrace();
		}
		if (dict != null) {
		    String prefix = getPrefix(viewer.getDocument(), offset);
		    TreeMap<String, ICompletionProposal> proposals = new TreeMap<String, ICompletionProposal>(dict.getProposals(prefix, activeFileName, offset));
		    // Add common powershell commandlets
		    if (Activator.getDefault().getPluginPreferences().getBoolean(PreferenceConstants.INCLUDE_PSCMDLETS)) {
		        proposals.putAll(dict.getProposals(prefix, CompletionDictionary.PS_CMDLET_FILENAME, offset));
		    }
		    // Add custom cmdlets
            if (Activator.getDefault().getPluginPreferences().getBoolean(PreferenceConstants.INCLUDE_CUSTOMCMDLETS)) {
                proposals.putAll(dict.getProposals(prefix, CompletionDictionary.CUSTOM_CMDLET_FILENAME, offset));
            }
		    ICompletionProposal[] result = new ICompletionProposal[proposals.values().size()];
		    proposals.values().toArray(result);
		    return result;
		}
		return emptyProposal;
	}
	
	private String getPrefix(IDocument doc, int offset) {
	    char ch;
	    try {
	    for (int i = offset - 1; i > 0; i--) {
	        ch = doc.getChar(i);
	        if (Character.isWhitespace(ch)) {
	            return doc.get(i + 1, offset - i - 1);
	        }
	    }
	    } catch (BadLocationException ble) {
	        ble.printStackTrace();
	    }
	    
	    return null;
	}

	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[] {':'};
	}

	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

	public String getErrorMessage() {
		return null;
	}

}
